plyr: Split-Apply-Combine

Why use apply functions instead of for loops?

plyr basics

  1. d = data frame.

  2. a = array (includes matrices).

  3. l = list.

  1. dataframe ddply ldply adply

  2. list dlply llply alply

  3. array daply laply aaply

  1. m = multi-argument function input.

  2. r = replicate a function n times.

  3. _ = throw away the output.

Base R apply functions and plyr

A general example with plyr

set.seed(1)
d <- data.frame(year = rep(2000:2002, each = 3),
  count = round(runif(9, 0, 20)))
print(d)
  year count
1 2000     5
2 2000     7
3 2000    11
4 2001    18
5 2001     4
6 2001    18
7 2002    19
8 2002    13
9 2002    13
library(plyr)
ddply(d, "year", function(x) {
  mean.count <- mean(x$count)
  sd.count <- sd(x$count)
  cv <- sd.count/mean.count
  data.frame(cv.count = cv)
})
  year  cv.count
1 2000 0.3984848
2 2001 0.6062178
3 2002 0.2309401

transform and summarise

ddply(d, "year", summarise, mean.count = mean(count))
  year mean.count
1 2000   7.666667
2 2001  13.333333
3 2002  15.000000
ddply(d, "year", transform, total.count = sum(count))
  year count total.count
1 2000     5          23
2 2000     7          23
3 2000    11          23
4 2001    18          40
5 2001     4          40
6 2001    18          40
7 2002    19          45
8 2002    13          45
9 2002    13          45
ddply(d, "year", mutate, mu = mean(count), sigma = sd(count),
  cv = sigma/mu)
  year count        mu    sigma        cv
1 2000     5  7.666667 3.055050 0.3984848
2 2000     7  7.666667 3.055050 0.3984848
3 2000    11  7.666667 3.055050 0.3984848
4 2001    18 13.333333 8.082904 0.6062178
5 2001     4 13.333333 8.082904 0.6062178
6 2001    18 13.333333 8.082904 0.6062178
7 2002    19 15.000000 3.464102 0.2309401
8 2002    13 15.000000 3.464102 0.2309401
9 2002    13 15.000000 3.464102 0.2309401

Plotting with plyr

par(mfrow = c(1, 3), mar = c(2, 2, 1, 1), oma = c(3, 3, 0, 0))
d_ply(d, "year", transform, plot(count, main = unique(year), type = "o"))
mtext("count", side = 1, outer = TRUE, line = 1)
mtext("frequency", side = 2, outer = TRUE, line = 1)

Nested chunking of the data

baseball.dat <- subset(baseball, year > 2000) # data from the plyr package
x <- ddply(baseball.dat, c("year", "team"), summarize,
  homeruns = sum(hr))
head(x)
  year team homeruns
1 2001  ANA        4
2 2001  ARI      155
3 2001  ATL       63
4 2001  BAL       58
5 2001  BOS       77
6 2001  CHA       63

Other useful options

Dealing with errors

  • You can use the failwith function to control how errors are dealt with.
f <- function(x) if (x == 1) stop("Error!") else 1
safe.f <- failwith(NA, f, quiet = TRUE)
# llply(1:2, f)
llply(1:2, safe.f)
[[1]]
[1] NA

[[2]]
[1] 1

Parallel processing

x <- c(1:10)
wait <- function(i) Sys.sleep(0.1)
system.time(llply(x, wait))
   user  system elapsed 
      0       0       1 
system.time(sapply(x, wait))
   user  system elapsed 
      0       0       1 

So, why would I not want to use plyr?

Some examples using the package plyr

library(plyr)
#Example dataset from ggplot
library(ggplot2)
data(mpg)
str(mpg)
Classes 'tbl_df', 'tbl' and 'data.frame':   234 obs. of  11 variables:
 $ manufacturer: chr  "audi" "audi" "audi" "audi" ...
 $ model       : chr  "a4" "a4" "a4" "a4" ...
 $ displ       : num  1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
 $ year        : int  1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
 $ cyl         : int  4 4 4 4 6 6 6 4 4 4 ...
 $ trans       : chr  "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
 $ drv         : chr  "f" "f" "f" "f" ...
 $ cty         : int  18 21 20 21 16 18 18 18 16 20 ...
 $ hwy         : int  29 29 31 30 26 26 27 26 25 28 ...
 $ fl          : chr  "p" "p" "p" "p" ...
 $ class       : chr  "compact" "compact" "compact" "compact" ...
#Simplify the dataset
data <- mpg[,c(1,7:9)]
str(data)
Classes 'tbl_df', 'tbl' and 'data.frame':   234 obs. of  4 variables:
 $ manufacturer: chr  "audi" "audi" "audi" "audi" ...
 $ drv         : chr  "f" "f" "f" "f" ...
 $ cty         : int  18 21 20 21 16 18 18 18 16 20 ...
 $ hwy         : int  29 29 31 30 26 26 27 26 25 28 ...
# Summarising/ Aggregating Data

ddply(data, .(manufacturer), summarize, avgcty = mean(cty))
   manufacturer   avgcty
1          audi 17.61111
2     chevrolet 15.00000
3         dodge 13.13514
4          ford 14.00000
5         honda 24.44444
6       hyundai 18.64286
7          jeep 13.50000
8    land rover 11.50000
9       lincoln 11.33333
10      mercury 13.25000
11       nissan 18.07692
12      pontiac 17.00000
13       subaru 19.28571
14       toyota 18.52941
15   volkswagen 20.92593
# you can perform multiple functions in a single call

ddply(data, .(manufacturer), summarize, avgcty = mean(cty), sdcty = sd(cty), maxhwy = max(hwy))
   manufacturer   avgcty     sdcty maxhwy
1          audi 17.61111 1.9745108     31
2     chevrolet 15.00000 2.9249881     30
3         dodge 13.13514 2.4850907     24
4          ford 14.00000 1.9148542     26
5         honda 24.44444 1.9436506     36
6       hyundai 18.64286 1.4990840     31
7          jeep 13.50000 2.5071327     22
8    land rover 11.50000 0.5773503     18
9       lincoln 11.33333 0.5773503     18
10      mercury 13.25000 0.5000000     19
11       nissan 18.07692 3.4268921     32
12      pontiac 17.00000 1.0000000     28
13       subaru 19.28571 0.9138735     27
14       toyota 18.52941 4.0469614     37
15   volkswagen 20.92593 4.5567020     44
# you can summarize data by a combination of variables/factors

ddply(data, .(manufacturer, drv), summarize, avgcty = mean(cty), sdcty = sd(cty), maxhwy = max(hwy))
   manufacturer drv   avgcty     sdcty maxhwy
1          audi   4 16.81818 1.6624188     28
2          audi   f 18.85714 1.8644545     31
3     chevrolet   4 12.50000 1.7320508     19
4     chevrolet   f 18.80000 1.9235384     30
5     chevrolet   r 14.10000 1.6633300     26
6         dodge   4 12.00000 1.7435596     19
7         dodge   f 15.81818 1.8340219     24
8          ford   4 13.30769 0.9473309     19
9          ford   r 14.75000 2.4167973     26
10        honda   f 24.44444 1.9436506     36
11      hyundai   f 18.64286 1.4990840     31
12         jeep   4 13.50000 2.5071327     22
13   land rover   4 11.50000 0.5773503     18
14      lincoln   r 11.33333 0.5773503     18
15      mercury   4 13.25000 0.5000000     19
16       nissan   4 13.75000 1.2583057     20
17       nissan   f 20.00000 1.8708287     32
18      pontiac   f 17.00000 1.0000000     28
19       subaru   4 19.28571 0.9138735     27
20       toyota   4 14.93333 1.4375906     22
21       toyota   f 21.36842 3.0223340     37
22   volkswagen   f 20.92593 4.5567020     44
# note the package reshape/reshape2 is an elegant alternative for aggregating many variables at one time

# note the differences between the commands "summarize" and "transform"

ddply(data, .(drv), summarize, avgcty = mean(cty))
  drv  avgcty
1   4 14.3301
2   f 19.9717
3   r 14.0800
ddply(data, .(drv), transform, avgcty = mean(cty))
    manufacturer drv cty hwy  avgcty
1           audi   4  18  26 14.3301
2           audi   4  16  25 14.3301
3           audi   4  20  28 14.3301
4           audi   4  19  27 14.3301
5           audi   4  15  25 14.3301
6           audi   4  17  25 14.3301
7           audi   4  17  25 14.3301
8           audi   4  15  25 14.3301
9           audi   4  15  24 14.3301
10          audi   4  17  25 14.3301
11          audi   4  16  23 14.3301
12     chevrolet   4  14  19 14.3301
13     chevrolet   4  11  14 14.3301
14     chevrolet   4  11  15 14.3301
15     chevrolet   4  14  17 14.3301
16         dodge   4  15  19 14.3301
17         dodge   4  14  18 14.3301
18         dodge   4  13  17 14.3301
19         dodge   4  14  17 14.3301
20         dodge   4  14  19 14.3301
21         dodge   4  14  19 14.3301
22         dodge   4   9  12 14.3301
23         dodge   4  11  17 14.3301
24         dodge   4  11  15 14.3301
25         dodge   4  13  17 14.3301
26         dodge   4  13  17 14.3301
27         dodge   4   9  12 14.3301
28         dodge   4  13  17 14.3301
29         dodge   4  11  16 14.3301
30         dodge   4  13  18 14.3301
31         dodge   4  11  15 14.3301
32         dodge   4  12  16 14.3301
33         dodge   4   9  12 14.3301
34         dodge   4  13  17 14.3301
35         dodge   4  13  17 14.3301
36         dodge   4  12  16 14.3301
37         dodge   4   9  12 14.3301
38         dodge   4  11  15 14.3301
39         dodge   4  11  16 14.3301
40         dodge   4  13  17 14.3301
41         dodge   4  11  15 14.3301
42          ford   4  14  17 14.3301
43          ford   4  15  19 14.3301
44          ford   4  14  17 14.3301
45          ford   4  13  19 14.3301
46          ford   4  13  19 14.3301
47          ford   4  13  17 14.3301
48          ford   4  14  17 14.3301
49          ford   4  14  17 14.3301
50          ford   4  13  16 14.3301
51          ford   4  13  16 14.3301
52          ford   4  13  17 14.3301
53          ford   4  11  15 14.3301
54          ford   4  13  17 14.3301
55          jeep   4  17  22 14.3301
56          jeep   4  15  19 14.3301
57          jeep   4  15  20 14.3301
58          jeep   4  14  17 14.3301
59          jeep   4   9  12 14.3301
60          jeep   4  14  19 14.3301
61          jeep   4  13  18 14.3301
62          jeep   4  11  14 14.3301
63    land rover   4  11  15 14.3301
64    land rover   4  12  18 14.3301
65    land rover   4  12  18 14.3301
66    land rover   4  11  15 14.3301
67       mercury   4  14  17 14.3301
68       mercury   4  13  19 14.3301
69       mercury   4  13  19 14.3301
70       mercury   4  13  17 14.3301
71        nissan   4  14  17 14.3301
72        nissan   4  15  17 14.3301
73        nissan   4  14  20 14.3301
74        nissan   4  12  18 14.3301
75        subaru   4  18  25 14.3301
76        subaru   4  18  24 14.3301
77        subaru   4  20  27 14.3301
78        subaru   4  19  25 14.3301
79        subaru   4  20  26 14.3301
80        subaru   4  18  23 14.3301
81        subaru   4  21  26 14.3301
82        subaru   4  19  26 14.3301
83        subaru   4  19  26 14.3301
84        subaru   4  19  26 14.3301
85        subaru   4  20  25 14.3301
86        subaru   4  20  27 14.3301
87        subaru   4  19  25 14.3301
88        subaru   4  20  27 14.3301
89        toyota   4  15  20 14.3301
90        toyota   4  16  20 14.3301
91        toyota   4  15  19 14.3301
92        toyota   4  15  17 14.3301
93        toyota   4  16  20 14.3301
94        toyota   4  14  17 14.3301
95        toyota   4  11  15 14.3301
96        toyota   4  13  18 14.3301
97        toyota   4  15  20 14.3301
98        toyota   4  16  20 14.3301
99        toyota   4  17  22 14.3301
100       toyota   4  15  17 14.3301
101       toyota   4  15  19 14.3301
102       toyota   4  15  18 14.3301
103       toyota   4  16  20 14.3301
104         audi   f  18  29 19.9717
105         audi   f  21  29 19.9717
106         audi   f  20  31 19.9717
107         audi   f  21  30 19.9717
108         audi   f  16  26 19.9717
109         audi   f  18  26 19.9717
110         audi   f  18  27 19.9717
111    chevrolet   f  19  27 19.9717
112    chevrolet   f  22  30 19.9717
113    chevrolet   f  18  26 19.9717
114    chevrolet   f  18  29 19.9717
115    chevrolet   f  17  26 19.9717
116        dodge   f  18  24 19.9717
117        dodge   f  17  24 19.9717
118        dodge   f  16  22 19.9717
119        dodge   f  16  22 19.9717
120        dodge   f  17  24 19.9717
121        dodge   f  17  24 19.9717
122        dodge   f  11  17 19.9717
123        dodge   f  15  22 19.9717
124        dodge   f  15  21 19.9717
125        dodge   f  16  23 19.9717
126        dodge   f  16  23 19.9717
127        honda   f  28  33 19.9717
128        honda   f  24  32 19.9717
129        honda   f  25  32 19.9717
130        honda   f  23  29 19.9717
131        honda   f  24  32 19.9717
132        honda   f  26  34 19.9717
133        honda   f  25  36 19.9717
134        honda   f  24  36 19.9717
135        honda   f  21  29 19.9717
136      hyundai   f  18  26 19.9717
137      hyundai   f  18  27 19.9717
138      hyundai   f  21  30 19.9717
139      hyundai   f  21  31 19.9717
140      hyundai   f  18  26 19.9717
141      hyundai   f  18  26 19.9717
142      hyundai   f  19  28 19.9717
143      hyundai   f  19  26 19.9717
144      hyundai   f  19  29 19.9717
145      hyundai   f  20  28 19.9717
146      hyundai   f  20  27 19.9717
147      hyundai   f  17  24 19.9717
148      hyundai   f  16  24 19.9717
149      hyundai   f  17  24 19.9717
150       nissan   f  21  29 19.9717
151       nissan   f  19  27 19.9717
152       nissan   f  23  31 19.9717
153       nissan   f  23  32 19.9717
154       nissan   f  19  27 19.9717
155       nissan   f  19  26 19.9717
156       nissan   f  18  26 19.9717
157       nissan   f  19  25 19.9717
158       nissan   f  19  25 19.9717
159      pontiac   f  18  26 19.9717
160      pontiac   f  16  26 19.9717
161      pontiac   f  17  27 19.9717
162      pontiac   f  18  28 19.9717
163      pontiac   f  16  25 19.9717
164       toyota   f  21  29 19.9717
165       toyota   f  21  27 19.9717
166       toyota   f  21  31 19.9717
167       toyota   f  21  31 19.9717
168       toyota   f  18  26 19.9717
169       toyota   f  18  26 19.9717
170       toyota   f  19  28 19.9717
171       toyota   f  21  27 19.9717
172       toyota   f  21  29 19.9717
173       toyota   f  21  31 19.9717
174       toyota   f  22  31 19.9717
175       toyota   f  18  26 19.9717
176       toyota   f  18  26 19.9717
177       toyota   f  18  27 19.9717
178       toyota   f  24  30 19.9717
179       toyota   f  24  33 19.9717
180       toyota   f  26  35 19.9717
181       toyota   f  28  37 19.9717
182       toyota   f  26  35 19.9717
183   volkswagen   f  21  29 19.9717
184   volkswagen   f  19  26 19.9717
185   volkswagen   f  21  29 19.9717
186   volkswagen   f  22  29 19.9717
187   volkswagen   f  17  24 19.9717
188   volkswagen   f  33  44 19.9717
189   volkswagen   f  21  29 19.9717
190   volkswagen   f  19  26 19.9717
191   volkswagen   f  22  29 19.9717
192   volkswagen   f  21  29 19.9717
193   volkswagen   f  21  29 19.9717
194   volkswagen   f  21  29 19.9717
195   volkswagen   f  16  23 19.9717
196   volkswagen   f  17  24 19.9717
197   volkswagen   f  35  44 19.9717
198   volkswagen   f  29  41 19.9717
199   volkswagen   f  21  29 19.9717
200   volkswagen   f  19  26 19.9717
201   volkswagen   f  20  28 19.9717
202   volkswagen   f  20  29 19.9717
203   volkswagen   f  21  29 19.9717
204   volkswagen   f  18  29 19.9717
205   volkswagen   f  19  28 19.9717
206   volkswagen   f  21  29 19.9717
207   volkswagen   f  16  26 19.9717
208   volkswagen   f  18  26 19.9717
209   volkswagen   f  17  26 19.9717
210    chevrolet   r  14  20 14.0800
211    chevrolet   r  11  15 14.0800
212    chevrolet   r  14  20 14.0800
213    chevrolet   r  13  17 14.0800
214    chevrolet   r  12  17 14.0800
215    chevrolet   r  16  26 14.0800
216    chevrolet   r  15  23 14.0800
217    chevrolet   r  16  26 14.0800
218    chevrolet   r  15  25 14.0800
219    chevrolet   r  15  24 14.0800
220         ford   r  11  17 14.0800
221         ford   r  11  17 14.0800
222         ford   r  12  18 14.0800
223         ford   r  18  26 14.0800
224         ford   r  18  25 14.0800
225         ford   r  17  26 14.0800
226         ford   r  16  24 14.0800
227         ford   r  15  21 14.0800
228         ford   r  15  22 14.0800
229         ford   r  15  23 14.0800
230         ford   r  15  22 14.0800
231         ford   r  14  20 14.0800
232      lincoln   r  11  17 14.0800
233      lincoln   r  11  16 14.0800
234      lincoln   r  12  18 14.0800
# transform is very useful standardizing/normalizing

ddply(data, .(drv), transform, delta = mean(cty)-cty)
    manufacturer drv cty hwy        delta
1           audi   4  18  26  -3.66990291
2           audi   4  16  25  -1.66990291
3           audi   4  20  28  -5.66990291
4           audi   4  19  27  -4.66990291
5           audi   4  15  25  -0.66990291
6           audi   4  17  25  -2.66990291
7           audi   4  17  25  -2.66990291
8           audi   4  15  25  -0.66990291
9           audi   4  15  24  -0.66990291
10          audi   4  17  25  -2.66990291
11          audi   4  16  23  -1.66990291
12     chevrolet   4  14  19   0.33009709
13     chevrolet   4  11  14   3.33009709
14     chevrolet   4  11  15   3.33009709
15     chevrolet   4  14  17   0.33009709
16         dodge   4  15  19  -0.66990291
17         dodge   4  14  18   0.33009709
18         dodge   4  13  17   1.33009709
19         dodge   4  14  17   0.33009709
20         dodge   4  14  19   0.33009709
21         dodge   4  14  19   0.33009709
22         dodge   4   9  12   5.33009709
23         dodge   4  11  17   3.33009709
24         dodge   4  11  15   3.33009709
25         dodge   4  13  17   1.33009709
26         dodge   4  13  17   1.33009709
27         dodge   4   9  12   5.33009709
28         dodge   4  13  17   1.33009709
29         dodge   4  11  16   3.33009709
30         dodge   4  13  18   1.33009709
31         dodge   4  11  15   3.33009709
32         dodge   4  12  16   2.33009709
33         dodge   4   9  12   5.33009709
34         dodge   4  13  17   1.33009709
35         dodge   4  13  17   1.33009709
36         dodge   4  12  16   2.33009709
37         dodge   4   9  12   5.33009709
38         dodge   4  11  15   3.33009709
39         dodge   4  11  16   3.33009709
40         dodge   4  13  17   1.33009709
41         dodge   4  11  15   3.33009709
42          ford   4  14  17   0.33009709
43          ford   4  15  19  -0.66990291
44          ford   4  14  17   0.33009709
45          ford   4  13  19   1.33009709
46          ford   4  13  19   1.33009709
47          ford   4  13  17   1.33009709
48          ford   4  14  17   0.33009709
49          ford   4  14  17   0.33009709
50          ford   4  13  16   1.33009709
51          ford   4  13  16   1.33009709
52          ford   4  13  17   1.33009709
53          ford   4  11  15   3.33009709
54          ford   4  13  17   1.33009709
55          jeep   4  17  22  -2.66990291
56          jeep   4  15  19  -0.66990291
57          jeep   4  15  20  -0.66990291
58          jeep   4  14  17   0.33009709
59          jeep   4   9  12   5.33009709
60          jeep   4  14  19   0.33009709
61          jeep   4  13  18   1.33009709
62          jeep   4  11  14   3.33009709
63    land rover   4  11  15   3.33009709
64    land rover   4  12  18   2.33009709
65    land rover   4  12  18   2.33009709
66    land rover   4  11  15   3.33009709
67       mercury   4  14  17   0.33009709
68       mercury   4  13  19   1.33009709
69       mercury   4  13  19   1.33009709
70       mercury   4  13  17   1.33009709
71        nissan   4  14  17   0.33009709
72        nissan   4  15  17  -0.66990291
73        nissan   4  14  20   0.33009709
74        nissan   4  12  18   2.33009709
75        subaru   4  18  25  -3.66990291
76        subaru   4  18  24  -3.66990291
77        subaru   4  20  27  -5.66990291
78        subaru   4  19  25  -4.66990291
79        subaru   4  20  26  -5.66990291
80        subaru   4  18  23  -3.66990291
81        subaru   4  21  26  -6.66990291
82        subaru   4  19  26  -4.66990291
83        subaru   4  19  26  -4.66990291
84        subaru   4  19  26  -4.66990291
85        subaru   4  20  25  -5.66990291
86        subaru   4  20  27  -5.66990291
87        subaru   4  19  25  -4.66990291
88        subaru   4  20  27  -5.66990291
89        toyota   4  15  20  -0.66990291
90        toyota   4  16  20  -1.66990291
91        toyota   4  15  19  -0.66990291
92        toyota   4  15  17  -0.66990291
93        toyota   4  16  20  -1.66990291
94        toyota   4  14  17   0.33009709
95        toyota   4  11  15   3.33009709
96        toyota   4  13  18   1.33009709
97        toyota   4  15  20  -0.66990291
98        toyota   4  16  20  -1.66990291
99        toyota   4  17  22  -2.66990291
100       toyota   4  15  17  -0.66990291
101       toyota   4  15  19  -0.66990291
102       toyota   4  15  18  -0.66990291
103       toyota   4  16  20  -1.66990291
104         audi   f  18  29   1.97169811
105         audi   f  21  29  -1.02830189
106         audi   f  20  31  -0.02830189
107         audi   f  21  30  -1.02830189
108         audi   f  16  26   3.97169811
109         audi   f  18  26   1.97169811
110         audi   f  18  27   1.97169811
111    chevrolet   f  19  27   0.97169811
112    chevrolet   f  22  30  -2.02830189
113    chevrolet   f  18  26   1.97169811
114    chevrolet   f  18  29   1.97169811
115    chevrolet   f  17  26   2.97169811
116        dodge   f  18  24   1.97169811
117        dodge   f  17  24   2.97169811
118        dodge   f  16  22   3.97169811
119        dodge   f  16  22   3.97169811
120        dodge   f  17  24   2.97169811
121        dodge   f  17  24   2.97169811
122        dodge   f  11  17   8.97169811
123        dodge   f  15  22   4.97169811
124        dodge   f  15  21   4.97169811
125        dodge   f  16  23   3.97169811
126        dodge   f  16  23   3.97169811
127        honda   f  28  33  -8.02830189
128        honda   f  24  32  -4.02830189
129        honda   f  25  32  -5.02830189
130        honda   f  23  29  -3.02830189
131        honda   f  24  32  -4.02830189
132        honda   f  26  34  -6.02830189
133        honda   f  25  36  -5.02830189
134        honda   f  24  36  -4.02830189
135        honda   f  21  29  -1.02830189
136      hyundai   f  18  26   1.97169811
137      hyundai   f  18  27   1.97169811
138      hyundai   f  21  30  -1.02830189
139      hyundai   f  21  31  -1.02830189
140      hyundai   f  18  26   1.97169811
141      hyundai   f  18  26   1.97169811
142      hyundai   f  19  28   0.97169811
143      hyundai   f  19  26   0.97169811
144      hyundai   f  19  29   0.97169811
145      hyundai   f  20  28  -0.02830189
146      hyundai   f  20  27  -0.02830189
147      hyundai   f  17  24   2.97169811
148      hyundai   f  16  24   3.97169811
149      hyundai   f  17  24   2.97169811
150       nissan   f  21  29  -1.02830189
151       nissan   f  19  27   0.97169811
152       nissan   f  23  31  -3.02830189
153       nissan   f  23  32  -3.02830189
154       nissan   f  19  27   0.97169811
155       nissan   f  19  26   0.97169811
156       nissan   f  18  26   1.97169811
157       nissan   f  19  25   0.97169811
158       nissan   f  19  25   0.97169811
159      pontiac   f  18  26   1.97169811
160      pontiac   f  16  26   3.97169811
161      pontiac   f  17  27   2.97169811
162      pontiac   f  18  28   1.97169811
163      pontiac   f  16  25   3.97169811
164       toyota   f  21  29  -1.02830189
165       toyota   f  21  27  -1.02830189
166       toyota   f  21  31  -1.02830189
167       toyota   f  21  31  -1.02830189
168       toyota   f  18  26   1.97169811
169       toyota   f  18  26   1.97169811
170       toyota   f  19  28   0.97169811
171       toyota   f  21  27  -1.02830189
172       toyota   f  21  29  -1.02830189
173       toyota   f  21  31  -1.02830189
174       toyota   f  22  31  -2.02830189
175       toyota   f  18  26   1.97169811
176       toyota   f  18  26   1.97169811
177       toyota   f  18  27   1.97169811
178       toyota   f  24  30  -4.02830189
179       toyota   f  24  33  -4.02830189
180       toyota   f  26  35  -6.02830189
181       toyota   f  28  37  -8.02830189
182       toyota   f  26  35  -6.02830189
183   volkswagen   f  21  29  -1.02830189
184   volkswagen   f  19  26   0.97169811
185   volkswagen   f  21  29  -1.02830189
186   volkswagen   f  22  29  -2.02830189
187   volkswagen   f  17  24   2.97169811
188   volkswagen   f  33  44 -13.02830189
189   volkswagen   f  21  29  -1.02830189
190   volkswagen   f  19  26   0.97169811
191   volkswagen   f  22  29  -2.02830189
192   volkswagen   f  21  29  -1.02830189
193   volkswagen   f  21  29  -1.02830189
194   volkswagen   f  21  29  -1.02830189
195   volkswagen   f  16  23   3.97169811
196   volkswagen   f  17  24   2.97169811
197   volkswagen   f  35  44 -15.02830189
198   volkswagen   f  29  41  -9.02830189
199   volkswagen   f  21  29  -1.02830189
200   volkswagen   f  19  26   0.97169811
201   volkswagen   f  20  28  -0.02830189
202   volkswagen   f  20  29  -0.02830189
203   volkswagen   f  21  29  -1.02830189
204   volkswagen   f  18  29   1.97169811
205   volkswagen   f  19  28   0.97169811
206   volkswagen   f  21  29  -1.02830189
207   volkswagen   f  16  26   3.97169811
208   volkswagen   f  18  26   1.97169811
209   volkswagen   f  17  26   2.97169811
210    chevrolet   r  14  20   0.08000000
211    chevrolet   r  11  15   3.08000000
212    chevrolet   r  14  20   0.08000000
213    chevrolet   r  13  17   1.08000000
214    chevrolet   r  12  17   2.08000000
215    chevrolet   r  16  26  -1.92000000
216    chevrolet   r  15  23  -0.92000000
217    chevrolet   r  16  26  -1.92000000
218    chevrolet   r  15  25  -0.92000000
219    chevrolet   r  15  24  -0.92000000
220         ford   r  11  17   3.08000000
221         ford   r  11  17   3.08000000
222         ford   r  12  18   2.08000000
223         ford   r  18  26  -3.92000000
224         ford   r  18  25  -3.92000000
225         ford   r  17  26  -2.92000000
226         ford   r  16  24  -1.92000000
227         ford   r  15  21  -0.92000000
228         ford   r  15  22  -0.92000000
229         ford   r  15  23  -0.92000000
230         ford   r  15  22  -0.92000000
231         ford   r  14  20   0.08000000
232      lincoln   r  11  17   3.08000000
233      lincoln   r  11  16   3.08000000
234      lincoln   r  12  18   2.08000000
# Now let's use plyr to run a simple loop

# We'll ask the question: Does city mpg  differ between car manufacturers, for each class of    drivetrains (4x4, forward, or rear-wheel drive)? Let's try to automate these ANOVAs and extract the F-statistics and P-values from the ANOVAs.

# Step1: create function to run ANOVA

model <- function(data) { aov(cty~manufacturer, data=data) }

# Step 2: Use plyr to run model for each and create list (called anova.output) to store output for  each drivetrain. For dlply, the syntax means d for input data is data frame and l for output data is list.

anova.output <- dlply(data, .(drv), model)

# Step 3: Create function that tells R where to find F-statistic and P-value in the output within  the list. The output is somewhat hidden in this example- don't worry about the messy indexing  here-- what's important is that this just tells R where the F-stats and P-values are stored. 

juicy <- function(x) { c(summary(x)[[1]][["F value"]][[1]], 
    summary(x)[[1]][["Pr(>F)"]][[1]]) }

# Step 4: Extract components of model output from the list created in previous step. For ldply, the  syntax is: input is list and output is data frame. Note that since the input is a list, we don't  have to indicate the 2nd parameter (which variable(s) to apply the function to, as the default is  to apply function to all elements of the list.) 

ldply(anova.output, juicy)
  drv        V1           V2
1   4 30.419370 4.992020e-24
2   f  7.231693 1.741008e-07
3   r  3.432065 5.043200e-02
# The data frame shows F-statistics (V1) and P-values (V2) for the ANOVAs by drivetrain.

# We could always condense some of the above steps as well:

anova.output <- dlply(data, .(drv), function(data) aov(cty~manufacturer, data=data))
ldply(anova.output, function(x) { c(summary(x)[[1]][["F value"]][[1]], summary(x)[[1]][["Pr(>F)"]][[1]]) })
  drv        V1           V2
1   4 30.419370 4.992020e-24
2   f  7.231693 1.741008e-07
3   r  3.432065 5.043200e-02
# Note that there are many shortcuts that plyr uses, such as the functions colwis(), each() and splat(). You can always refer to the original article: http://www.jstatsoft.org/v40/i01/ for more on this.

Using gapminder dataset

library(gapminder)
library(plyr)
library(ggplot2)
data(gapminder)
str(gapminder)
Classes 'tbl_df', 'tbl' and 'data.frame':   1704 obs. of  6 variables:
 $ country  : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
 $ lifeExp  : num  28.8 30.3 32 34 36.1 ...
 $ pop      : int  8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
 $ gdpPercap: num  779 821 853 836 740 ...
head(gapminder)
# A tibble: 6 x 6
  country     continent  year lifeExp      pop gdpPercap
  <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
1 Afghanistan Asia       1952    28.8  8425333      779.
2 Afghanistan Asia       1957    30.3  9240934      821.
3 Afghanistan Asia       1962    32.0 10267083      853.
4 Afghanistan Asia       1967    34.0 11537966      836.
5 Afghanistan Asia       1972    36.1 13079460      740.
6 Afghanistan Asia       1977    38.4 14880372      786.

We want to know the number of countries

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
library(plotly)
data(gapminder)

get.n.countries<-function(x)length(unique(x$country))

#use daply
gap_country<-daply(gapminder,.(continent),get.n.countries)
gap_country
  Africa Americas     Asia   Europe  Oceania 
      52       25       33       30        2 
#use ddply
gap_country<-ddply(gapminder,.(continent),function(x)length(unique(x$country)))
gap_country
  continent V1
1    Africa 52
2  Americas 25
3      Asia 33
4    Europe 30
5   Oceania  2
#use ddply and plot a bargraph
gap_country<-ddply(gapminder,.(continent),summarise,totalcountries=length(unique(country)))
gap_country
  continent totalcountries
1    Africa             52
2  Americas             25
3      Asia             33
4    Europe             30
5   Oceania              2
g<-ggplot(gap_country,aes(x=continent,y=totalcountries,fill=continent))+geom_bar(stat="identity")+
geom_text(aes(label=gap_country$totalcountries),nudge_y=1)+xlab("Continent")+ylab("Total Countries")+
  ggtitle("Barplot of Total Countries in each Continent")
ggplotly(g)

Sum total population in a dataframe

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

#first write the function
get.total.pop<-function(x)sum(x$pop)

#use daply
gap_pop<-daply(gapminder,.(continent),get.total.pop)
gap_pop
     Africa    Americas        Asia      Europe     Oceania 
 6187585961  7351438499 30507333901  6181115304   212992136 
#use ddply
gap_pop<-ddply(gapminder,.(continent),get.total.pop)
gap_pop
  continent          V1
1    Africa  6187585961
2  Americas  7351438499
3      Asia 30507333901
4    Europe  6181115304
5   Oceania   212992136
#We note that the pop has been exagerated so we add another variable to the list of splitting criteria.
gap_pop<-ddply(gapminder,.(continent,year),get.total.pop)
gap_pop
   continent year         V1
1     Africa 1952  237640501
2     Africa 1957  264837738
3     Africa 1962  296516865
4     Africa 1967  335289489
5     Africa 1972  379879541
6     Africa 1977  433061021
7     Africa 1982  499348587
8     Africa 1987  574834110
9     Africa 1992  659081517
10    Africa 1997  743832984
11    Africa 2002  833723916
12    Africa 2007  929539692
13  Americas 1952  345152446
14  Americas 1957  386953916
15  Americas 1962  433270254
16  Americas 1967  480746623
17  Americas 1972  529384210
18  Americas 1977  578067699
19  Americas 1982  630290920
20  Americas 1987  682753971
21  Americas 1992  739274104
22  Americas 1997  796900410
23  Americas 2002  849772762
24  Americas 2007  898871184
25      Asia 1952 1395357351
26      Asia 1957 1562780599
27      Asia 1962 1696357182
28      Asia 1967 1905662900
29      Asia 1972 2150972248
30      Asia 1977 2384513556
31      Asia 1982 2610135582
32      Asia 1987 2871220762
33      Asia 1992 3133292191
34      Asia 1997 3383285500
35      Asia 2002 3601802203
36      Asia 2007 3811953827
37    Europe 1952  418120846
38    Europe 1957  437890351
39    Europe 1962  460355155
40    Europe 1967  481178958
41    Europe 1972  500635059
42    Europe 1977  517164531
43    Europe 1982  531266901
44    Europe 1987  543094160
45    Europe 1992  558142797
46    Europe 1997  568944148
47    Europe 2002  578223869
48    Europe 2007  586098529
49   Oceania 1952   10686006
50   Oceania 1957   11941976
51   Oceania 1962   13283518
52   Oceania 1967   14600414
53   Oceania 1972   16106100
54   Oceania 1977   17239000
55   Oceania 1982   18394850
56   Oceania 1987   19574415
57   Oceania 1992   20919651
58   Oceania 1997   22241430
59   Oceania 2002   23454829
60   Oceania 2007   24549947

Maximum gdpPercap on each continent

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

max_gdp<-ddply(gapminder,.(continent,year),function(x)max(x$gdpPercap))
max_gdp
   continent year         V1
1     Africa 1952   4725.296
2     Africa 1957   5487.104
3     Africa 1962   6757.031
4     Africa 1967  18772.752
5     Africa 1972  21011.497
6     Africa 1977  21951.212
7     Africa 1982  17364.275
8     Africa 1987  11864.408
9     Africa 1992  13522.158
10    Africa 1997  14722.842
11    Africa 2002  12521.714
12    Africa 2007  13206.485
13  Americas 1952  13990.482
14  Americas 1957  14847.127
15  Americas 1962  16173.146
16  Americas 1967  19530.366
17  Americas 1972  21806.036
18  Americas 1977  24072.632
19  Americas 1982  25009.559
20  Americas 1987  29884.350
21  Americas 1992  32003.932
22  Americas 1997  35767.433
23  Americas 2002  39097.100
24  Americas 2007  42951.653
25      Asia 1952 108382.353
26      Asia 1957 113523.133
27      Asia 1962  95458.112
28      Asia 1967  80894.883
29      Asia 1972 109347.867
30      Asia 1977  59265.477
31      Asia 1982  33693.175
32      Asia 1987  28118.430
33      Asia 1992  34932.920
34      Asia 1997  40300.620
35      Asia 2002  36023.105
36      Asia 2007  47306.990
37    Europe 1952  14734.233
38    Europe 1957  17909.490
39    Europe 1962  20431.093
40    Europe 1967  22966.144
41    Europe 1972  27195.113
42    Europe 1977  26982.291
43    Europe 1982  28397.715
44    Europe 1987  31540.975
45    Europe 1992  33965.661
46    Europe 1997  41283.164
47    Europe 2002  44683.975
48    Europe 2007  49357.190
49   Oceania 1952  10556.576
50   Oceania 1957  12247.395
51   Oceania 1962  13175.678
52   Oceania 1967  14526.125
53   Oceania 1972  16788.629
54   Oceania 1977  18334.198
55   Oceania 1982  19477.009
56   Oceania 1987  21888.889
57   Oceania 1992  23424.767
58   Oceania 1997  26997.937
59   Oceania 2002  30687.755
60   Oceania 2007  34435.367
DT::datatable(max_gdp,options=list(pageLength=10))

An example returning a list

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

get.countries<-function(x)unique(x$country)

#use dlply
countries<-dlply(gapminder,.(continent),function(x)unique(x$country))
countries
$Africa
 [1] Algeria                  Angola                   Benin                   
 [4] Botswana                 Burkina Faso             Burundi                 
 [7] Cameroon                 Central African Republic Chad                    
[10] Comoros                  Congo, Dem. Rep.         Congo, Rep.             
[13] Cote d'Ivoire            Djibouti                 Egypt                   
[16] Equatorial Guinea        Eritrea                  Ethiopia                
[19] Gabon                    Gambia                   Ghana                   
[22] Guinea                   Guinea-Bissau            Kenya                   
[25] Lesotho                  Liberia                  Libya                   
[28] Madagascar               Malawi                   Mali                    
[31] Mauritania               Mauritius                Morocco                 
[34] Mozambique               Namibia                  Niger                   
[37] Nigeria                  Reunion                  Rwanda                  
[40] Sao Tome and Principe    Senegal                  Sierra Leone            
[43] Somalia                  South Africa             Sudan                   
[46] Swaziland                Tanzania                 Togo                    
[49] Tunisia                  Uganda                   Zambia                  
[52] Zimbabwe                
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

$Americas
 [1] Argentina           Bolivia             Brazil             
 [4] Canada              Chile               Colombia           
 [7] Costa Rica          Cuba                Dominican Republic 
[10] Ecuador             El Salvador         Guatemala          
[13] Haiti               Honduras            Jamaica            
[16] Mexico              Nicaragua           Panama             
[19] Paraguay            Peru                Puerto Rico        
[22] Trinidad and Tobago United States       Uruguay            
[25] Venezuela          
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

$Asia
 [1] Afghanistan        Bahrain            Bangladesh         Cambodia          
 [5] China              Hong Kong, China   India              Indonesia         
 [9] Iran               Iraq               Israel             Japan             
[13] Jordan             Korea, Dem. Rep.   Korea, Rep.        Kuwait            
[17] Lebanon            Malaysia           Mongolia           Myanmar           
[21] Nepal              Oman               Pakistan           Philippines       
[25] Saudi Arabia       Singapore          Sri Lanka          Syria             
[29] Taiwan             Thailand           Vietnam            West Bank and Gaza
[33] Yemen, Rep.       
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

$Europe
 [1] Albania                Austria                Belgium               
 [4] Bosnia and Herzegovina Bulgaria               Croatia               
 [7] Czech Republic         Denmark                Finland               
[10] France                 Germany                Greece                
[13] Hungary                Iceland                Ireland               
[16] Italy                  Montenegro             Netherlands           
[19] Norway                 Poland                 Portugal              
[22] Romania                Serbia                 Slovak Republic       
[25] Slovenia               Spain                  Sweden                
[28] Switzerland            Turkey                 United Kingdom        
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

$Oceania
[1] Australia   New Zealand
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
  continent
1    Africa
2  Americas
3      Asia
4    Europe
5   Oceania
countries<-dlply(gapminder,.(continent),get.countries)
countries
$Africa
 [1] Algeria                  Angola                   Benin                   
 [4] Botswana                 Burkina Faso             Burundi                 
 [7] Cameroon                 Central African Republic Chad                    
[10] Comoros                  Congo, Dem. Rep.         Congo, Rep.             
[13] Cote d'Ivoire            Djibouti                 Egypt                   
[16] Equatorial Guinea        Eritrea                  Ethiopia                
[19] Gabon                    Gambia                   Ghana                   
[22] Guinea                   Guinea-Bissau            Kenya                   
[25] Lesotho                  Liberia                  Libya                   
[28] Madagascar               Malawi                   Mali                    
[31] Mauritania               Mauritius                Morocco                 
[34] Mozambique               Namibia                  Niger                   
[37] Nigeria                  Reunion                  Rwanda                  
[40] Sao Tome and Principe    Senegal                  Sierra Leone            
[43] Somalia                  South Africa             Sudan                   
[46] Swaziland                Tanzania                 Togo                    
[49] Tunisia                  Uganda                   Zambia                  
[52] Zimbabwe                
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

$Americas
 [1] Argentina           Bolivia             Brazil             
 [4] Canada              Chile               Colombia           
 [7] Costa Rica          Cuba                Dominican Republic 
[10] Ecuador             El Salvador         Guatemala          
[13] Haiti               Honduras            Jamaica            
[16] Mexico              Nicaragua           Panama             
[19] Paraguay            Peru                Puerto Rico        
[22] Trinidad and Tobago United States       Uruguay            
[25] Venezuela          
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

$Asia
 [1] Afghanistan        Bahrain            Bangladesh         Cambodia          
 [5] China              Hong Kong, China   India              Indonesia         
 [9] Iran               Iraq               Israel             Japan             
[13] Jordan             Korea, Dem. Rep.   Korea, Rep.        Kuwait            
[17] Lebanon            Malaysia           Mongolia           Myanmar           
[21] Nepal              Oman               Pakistan           Philippines       
[25] Saudi Arabia       Singapore          Sri Lanka          Syria             
[29] Taiwan             Thailand           Vietnam            West Bank and Gaza
[33] Yemen, Rep.       
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

$Europe
 [1] Albania                Austria                Belgium               
 [4] Bosnia and Herzegovina Bulgaria               Croatia               
 [7] Czech Republic         Denmark                Finland               
[10] France                 Germany                Greece                
[13] Hungary                Iceland                Ireland               
[16] Italy                  Montenegro             Netherlands           
[19] Norway                 Poland                 Portugal              
[22] Romania                Serbia                 Slovak Republic       
[25] Slovenia               Spain                  Sweden                
[28] Switzerland            Turkey                 United Kingdom        
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

$Oceania
[1] Australia   New Zealand
142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe

attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
  continent
1    Africa
2  Americas
3      Asia
4    Europe
5   Oceania

Feed data into a model one by one returning fits to a list of models

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

model<-function(x){
  lm(lifeExp~log10(gdpPercap),data=gapminder)
}

#Now let's try it on a subset of data
fit<-model(data[data$year==1982 & data$continent=="Asia",])
fit

Call:
lm(formula = lifeExp ~ log10(gdpPercap), data = gapminder)

Coefficients:
     (Intercept)  log10(gdpPercap)  
          -9.101            19.353  
# Now let's apply it all continents in all years

fitted.linear.model<-dlply(gapminder,.(continent,year),model)

coef(fitted.linear.model[[1]])
     (Intercept) log10(gdpPercap) 
       -9.100889        19.353423 
ldply(fitted.linear.model,coef)
   continent year (Intercept) log10(gdpPercap)
1     Africa 1952   -9.100889         19.35342
2     Africa 1957   -9.100889         19.35342
3     Africa 1962   -9.100889         19.35342
4     Africa 1967   -9.100889         19.35342
5     Africa 1972   -9.100889         19.35342
6     Africa 1977   -9.100889         19.35342
7     Africa 1982   -9.100889         19.35342
8     Africa 1987   -9.100889         19.35342
9     Africa 1992   -9.100889         19.35342
10    Africa 1997   -9.100889         19.35342
11    Africa 2002   -9.100889         19.35342
12    Africa 2007   -9.100889         19.35342
13  Americas 1952   -9.100889         19.35342
14  Americas 1957   -9.100889         19.35342
15  Americas 1962   -9.100889         19.35342
16  Americas 1967   -9.100889         19.35342
17  Americas 1972   -9.100889         19.35342
18  Americas 1977   -9.100889         19.35342
19  Americas 1982   -9.100889         19.35342
20  Americas 1987   -9.100889         19.35342
21  Americas 1992   -9.100889         19.35342
22  Americas 1997   -9.100889         19.35342
23  Americas 2002   -9.100889         19.35342
24  Americas 2007   -9.100889         19.35342
25      Asia 1952   -9.100889         19.35342
26      Asia 1957   -9.100889         19.35342
27      Asia 1962   -9.100889         19.35342
28      Asia 1967   -9.100889         19.35342
29      Asia 1972   -9.100889         19.35342
30      Asia 1977   -9.100889         19.35342
31      Asia 1982   -9.100889         19.35342
32      Asia 1987   -9.100889         19.35342
33      Asia 1992   -9.100889         19.35342
34      Asia 1997   -9.100889         19.35342
35      Asia 2002   -9.100889         19.35342
36      Asia 2007   -9.100889         19.35342
37    Europe 1952   -9.100889         19.35342
38    Europe 1957   -9.100889         19.35342
39    Europe 1962   -9.100889         19.35342
40    Europe 1967   -9.100889         19.35342
41    Europe 1972   -9.100889         19.35342
42    Europe 1977   -9.100889         19.35342
43    Europe 1982   -9.100889         19.35342
44    Europe 1987   -9.100889         19.35342
45    Europe 1992   -9.100889         19.35342
46    Europe 1997   -9.100889         19.35342
47    Europe 2002   -9.100889         19.35342
48    Europe 2007   -9.100889         19.35342
49   Oceania 1952   -9.100889         19.35342
50   Oceania 1957   -9.100889         19.35342
51   Oceania 1962   -9.100889         19.35342
52   Oceania 1967   -9.100889         19.35342
53   Oceania 1972   -9.100889         19.35342
54   Oceania 1977   -9.100889         19.35342
55   Oceania 1982   -9.100889         19.35342
56   Oceania 1987   -9.100889         19.35342
57   Oceania 1992   -9.100889         19.35342
58   Oceania 1997   -9.100889         19.35342
59   Oceania 2002   -9.100889         19.35342
60   Oceania 2007   -9.100889         19.35342
#we want the r2 too
ldply(fitted.linear.model,function(x)summary(x)$r.squared)
   continent year        V1
1     Africa 1952 0.6522466
2     Africa 1957 0.6522466
3     Africa 1962 0.6522466
4     Africa 1967 0.6522466
5     Africa 1972 0.6522466
6     Africa 1977 0.6522466
7     Africa 1982 0.6522466
8     Africa 1987 0.6522466
9     Africa 1992 0.6522466
10    Africa 1997 0.6522466
11    Africa 2002 0.6522466
12    Africa 2007 0.6522466
13  Americas 1952 0.6522466
14  Americas 1957 0.6522466
15  Americas 1962 0.6522466
16  Americas 1967 0.6522466
17  Americas 1972 0.6522466
18  Americas 1977 0.6522466
19  Americas 1982 0.6522466
20  Americas 1987 0.6522466
21  Americas 1992 0.6522466
22  Americas 1997 0.6522466
23  Americas 2002 0.6522466
24  Americas 2007 0.6522466
25      Asia 1952 0.6522466
26      Asia 1957 0.6522466
27      Asia 1962 0.6522466
28      Asia 1967 0.6522466
29      Asia 1972 0.6522466
30      Asia 1977 0.6522466
31      Asia 1982 0.6522466
32      Asia 1987 0.6522466
33      Asia 1992 0.6522466
34      Asia 1997 0.6522466
35      Asia 2002 0.6522466
36      Asia 2007 0.6522466
37    Europe 1952 0.6522466
38    Europe 1957 0.6522466
39    Europe 1962 0.6522466
40    Europe 1967 0.6522466
41    Europe 1972 0.6522466
42    Europe 1977 0.6522466
43    Europe 1982 0.6522466
44    Europe 1987 0.6522466
45    Europe 1992 0.6522466
46    Europe 1997 0.6522466
47    Europe 2002 0.6522466
48    Europe 2007 0.6522466
49   Oceania 1952 0.6522466
50   Oceania 1957 0.6522466
51   Oceania 1962 0.6522466
52   Oceania 1967 0.6522466
53   Oceania 1972 0.6522466
54   Oceania 1977 0.6522466
55   Oceania 1982 0.6522466
56   Oceania 1987 0.6522466
57   Oceania 1992 0.6522466
58   Oceania 1997 0.6522466
59   Oceania 2002 0.6522466
60   Oceania 2007 0.6522466

We want model function to return the desired output

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

model<-function(x){
  fit<-lm(lifeExp~log10(gdpPercap),data=gapminder)
  data.frame(n=length(x$lifeExp),r2=summary(fit)$r.squared,a=coef(fit)[[1]],b=coef(fit)[[2]])
}

#Then apply ddply
ddply(gapminder,.(continent,year),model)
   continent year  n        r2         a        b
1     Africa 1952 52 0.6522466 -9.100889 19.35342
2     Africa 1957 52 0.6522466 -9.100889 19.35342
3     Africa 1962 52 0.6522466 -9.100889 19.35342
4     Africa 1967 52 0.6522466 -9.100889 19.35342
5     Africa 1972 52 0.6522466 -9.100889 19.35342
6     Africa 1977 52 0.6522466 -9.100889 19.35342
7     Africa 1982 52 0.6522466 -9.100889 19.35342
8     Africa 1987 52 0.6522466 -9.100889 19.35342
9     Africa 1992 52 0.6522466 -9.100889 19.35342
10    Africa 1997 52 0.6522466 -9.100889 19.35342
11    Africa 2002 52 0.6522466 -9.100889 19.35342
12    Africa 2007 52 0.6522466 -9.100889 19.35342
13  Americas 1952 25 0.6522466 -9.100889 19.35342
14  Americas 1957 25 0.6522466 -9.100889 19.35342
15  Americas 1962 25 0.6522466 -9.100889 19.35342
16  Americas 1967 25 0.6522466 -9.100889 19.35342
17  Americas 1972 25 0.6522466 -9.100889 19.35342
18  Americas 1977 25 0.6522466 -9.100889 19.35342
19  Americas 1982 25 0.6522466 -9.100889 19.35342
20  Americas 1987 25 0.6522466 -9.100889 19.35342
21  Americas 1992 25 0.6522466 -9.100889 19.35342
22  Americas 1997 25 0.6522466 -9.100889 19.35342
23  Americas 2002 25 0.6522466 -9.100889 19.35342
24  Americas 2007 25 0.6522466 -9.100889 19.35342
25      Asia 1952 33 0.6522466 -9.100889 19.35342
26      Asia 1957 33 0.6522466 -9.100889 19.35342
27      Asia 1962 33 0.6522466 -9.100889 19.35342
28      Asia 1967 33 0.6522466 -9.100889 19.35342
29      Asia 1972 33 0.6522466 -9.100889 19.35342
30      Asia 1977 33 0.6522466 -9.100889 19.35342
31      Asia 1982 33 0.6522466 -9.100889 19.35342
32      Asia 1987 33 0.6522466 -9.100889 19.35342
33      Asia 1992 33 0.6522466 -9.100889 19.35342
34      Asia 1997 33 0.6522466 -9.100889 19.35342
35      Asia 2002 33 0.6522466 -9.100889 19.35342
36      Asia 2007 33 0.6522466 -9.100889 19.35342
37    Europe 1952 30 0.6522466 -9.100889 19.35342
38    Europe 1957 30 0.6522466 -9.100889 19.35342
39    Europe 1962 30 0.6522466 -9.100889 19.35342
40    Europe 1967 30 0.6522466 -9.100889 19.35342
41    Europe 1972 30 0.6522466 -9.100889 19.35342
42    Europe 1977 30 0.6522466 -9.100889 19.35342
43    Europe 1982 30 0.6522466 -9.100889 19.35342
44    Europe 1987 30 0.6522466 -9.100889 19.35342
45    Europe 1992 30 0.6522466 -9.100889 19.35342
46    Europe 1997 30 0.6522466 -9.100889 19.35342
47    Europe 2002 30 0.6522466 -9.100889 19.35342
48    Europe 2007 30 0.6522466 -9.100889 19.35342
49   Oceania 1952  2 0.6522466 -9.100889 19.35342
50   Oceania 1957  2 0.6522466 -9.100889 19.35342
51   Oceania 1962  2 0.6522466 -9.100889 19.35342
52   Oceania 1967  2 0.6522466 -9.100889 19.35342
53   Oceania 1972  2 0.6522466 -9.100889 19.35342
54   Oceania 1977  2 0.6522466 -9.100889 19.35342
55   Oceania 1982  2 0.6522466 -9.100889 19.35342
56   Oceania 1987  2 0.6522466 -9.100889 19.35342
57   Oceania 1992  2 0.6522466 -9.100889 19.35342
58   Oceania 1997  2 0.6522466 -9.100889 19.35342
59   Oceania 2002  2 0.6522466 -9.100889 19.35342
60   Oceania 2007  2 0.6522466 -9.100889 19.35342

As a final extension

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

model<-function(d,x,y){
  fit<-lm(d[[y]]~log10(d[[x]]))
  data.frame(n=length(d[[y]]),r2=summary(fit)$r.squared,a=coef(fit)[1],b=coef(fit)[2])
}

ddply(gapminder,.(continent,year),model,y="lifeExp",x="gdpPercap")
   continent year  n        r2            a           b
1     Africa 1952 52 0.1037812   23.0525953    5.382298
2     Africa 1957 52 0.1148056   22.5199297    6.189243
3     Africa 1962 52 0.1194333   23.7915807    6.346554
4     Africa 1967 52 0.1616558   23.1749106    7.065407
5     Africa 1972 52 0.2380010   19.5713666    8.757875
6     Africa 1977 52 0.3245551   16.3845126   10.384037
7     Africa 1982 52 0.4389610   10.2567015   12.927044
8     Africa 1987 52 0.5101598    5.9844769   14.917624
9     Africa 1992 52 0.4683695    0.1812503   16.884333
10    Africa 1997 52 0.4689442    3.2574767   15.853737
11    Africa 2002 52 0.2657330   15.2988527   11.884759
12    Africa 2007 52 0.2038938   22.9068245    9.811029
13  Americas 1952 25 0.3911997  -31.6862405   24.041040
14  Americas 1957 25 0.4465440  -29.2582342   23.772381
15  Americas 1962 25 0.4761545  -28.1511355   23.930105
16  Americas 1967 25 0.4577251  -13.3036872   20.086358
17  Americas 1972 25 0.4956498  -10.3599462   19.508293
18  Americas 1977 25 0.4678976   -7.2373578   18.914422
19  Americas 1982 25 0.5584315   -7.4120953   19.409076
20  Americas 1987 25 0.6296113    6.1098075   16.344021
21  Americas 1992 25 0.6657980   16.6470136   13.942420
22  Americas 1997 25 0.6316171   23.5244087   12.421122
23  Americas 2002 25 0.6091473   27.7003029   11.639127
24  Americas 2007 25 0.6085279   33.0650797   10.347969
25      Asia 1952 33 0.2629815   15.8323119    9.578001
26      Asia 1957 33 0.2552922   18.0672456    9.604586
27      Asia 1962 33 0.3084034   16.5934165   10.574676
28      Asia 1967 33 0.3295700   19.7785273   10.365823
29      Asia 1972 33 0.3854367   21.9226137   10.234456
30      Asia 1977 33 0.4210308   20.0389431   11.219246
31      Asia 1982 33 0.5035094   23.3557733   11.004645
32      Asia 1987 33 0.6205701   22.0206839   11.914813
33      Asia 1992 33 0.6646179   24.0496786   11.716722
34      Asia 1997 33 0.6816437   24.7325393   11.772759
35      Asia 2002 33 0.6634051   22.7361333   12.533686
36      Asia 2007 33 0.6405559   25.6501149   11.875027
37    Europe 1952 30 0.7482006  -11.9466866   20.732600
38    Europe 1957 30 0.6892937    1.9131575   17.151109
39    Europe 1962 30 0.6317579   15.9752595   13.613689
40    Europe 1967 30 0.6240650   20.3352355   12.503058
41    Europe 1972 30 0.5585790   28.7981541   10.377387
42    Europe 1977 30 0.5572371   29.4891023   10.333333
43    Europe 1982 30 0.5215061   31.0971353   10.062767
44    Europe 1987 30 0.5200620   33.7253780    9.542663
45    Europe 1992 30 0.5932718   41.1793922    8.020074
46    Europe 1997 30 0.6627327   39.1376795    8.656197
47    Europe 2002 30 0.7158133   40.0208847    8.610798
48    Europe 2007 30 0.6986601   35.4389538    9.732777
49   Oceania 1952  2 1.0000000   19.5730133   12.381434
50   Oceania 1957  2 1.0000000   76.1428615   -1.439041
51   Oceania 1962  2 1.0000000   32.3035181    9.451123
52   Oceania 1967  2 1.0000000 1009.0303027 -225.347562
53   Oceania 1972  2 1.0000000   63.3283428    2.035888
54   Oceania 1977  2 1.0000000  -28.9691479   24.033041
55   Oceania 1982  2 1.0000000  -14.6031550   20.828230
56   Oceania 1987  2 1.0000000  -65.2726571   32.623355
57   Oceania 1992  2 1.0000000   26.7228887   11.634071
58   Oceania 1997  2 1.0000000   26.3445137   11.844182
59   Oceania 2002  2 1.0000000   33.9027938   10.356044
60   Oceania 2007  2 1.0000000   46.8057196    7.588549
ddply(gapminder,.(continent,year),model,y="lifeExp",x="pop")
   continent year  n           r2        a            b
1     Africa 1952 52 1.680740e-02 45.82174 -1.061114071
2     Africa 1957 52 1.647705e-02 48.51212 -1.141957809
3     Africa 1962 52 1.357602e-02 50.24610 -1.083610037
4     Africa 1967 52 1.244434e-02 52.25823 -1.074383509
5     Africa 1972 52 1.453069e-02 55.42445 -1.226755888
6     Africa 1977 52 1.543458e-02 58.33067 -1.334948680
7     Africa 1982 52 2.149108e-02 62.95431 -1.716991971
8     Africa 1987 52 2.318606e-02 65.97257 -1.891056448
9     Africa 1992 52 2.338874e-02 69.01281 -2.283980852
10    Africa 1997 52 2.816713e-02 69.96727 -2.411805750
11    Africa 2002 52 1.389859e-02 65.53089 -1.785103203
12    Africa 2007 52 1.021619e-02 65.35258 -1.532099416
13  Americas 1952 25 5.330530e-02 28.99425  3.658742281
14  Americas 1957 25 4.042921e-02 35.28231  3.089795762
15  Americas 1962 25 2.660386e-02 42.40781  2.369378399
16  Americas 1967 25 1.795992e-02 48.04260  1.817967119
17  Americas 1972 25 1.395788e-02 52.22941  1.483622772
18  Americas 1977 25 1.655218e-02 53.64259  1.558937684
19  Americas 1982 25 2.189085e-02 54.39069  1.706533371
20  Americas 1987 25 1.769986e-02 58.83659  1.326233344
21  Americas 1992 25 2.415249e-02 59.89817  1.378012025
22  Americas 1997 25 3.482957e-02 60.13455  1.561720628
23  Americas 2002 25 3.847289e-02 61.02259  1.608888299
24  Americas 2007 25 4.270098e-02 62.48011  1.564414213
25      Asia 1952 33 5.972720e-02 64.45784 -2.653709215
26      Asia 1957 33 3.630124e-02 64.28197 -2.170943716
27      Asia 1962 33 5.613702e-02 71.13631 -2.814381525
28      Asia 1967 33 2.715499e-02 68.35687 -1.951926642
29      Asia 1972 33 2.603796e-02 71.04963 -1.941551914
30      Asia 1977 33 2.409241e-02 73.54362 -1.954630399
31      Asia 1982 33 4.896702e-02 79.92524 -2.409630550
32      Asia 1987 33 5.841097e-02 83.35227 -2.557383185
33      Asia 1992 33 6.491277e-02 85.96943 -2.668992385
34      Asia 1997 33 6.089025e-02 87.34755 -2.636110591
35      Asia 2002 33 5.120642e-02 87.90039 -2.532215613
36      Asia 2007 33 5.608188e-02 89.59822 -2.546747792
37    Europe 1952 30 7.971462e-04 66.47946 -0.303116636
38    Europe 1957 30 2.173609e-03 69.57908 -0.419537642
39    Europe 1962 30 5.695284e-03 72.35425 -0.554674642
40    Europe 1967 30 9.268471e-03 74.06920 -0.627947029
41    Europe 1972 30 2.199767e-02 76.49081 -0.826584155
42    Europe 1977 30 4.558867e-02 79.91976 -1.151515813
43    Europe 1982 30 3.004874e-02 79.52002 -0.966738479
44    Europe 1987 30 2.270296e-02 79.41169 -0.829623099
45    Europe 1992 30 1.699916e-02 79.52509 -0.730052693
46    Europe 1997 30 2.382061e-03 77.35196 -0.264925698
47    Europe 2002 30 7.515142e-05 76.38955  0.044571189
48    Europe 2007 30 6.898088e-07 77.61818  0.004355314
49   Oceania 1952  2 1.000000e+00 72.05117 -0.422414707
50   Oceania 1957  2 1.000000e+00 69.56474  0.109521355
51   Oceania 1962  2 1.000000e+00 74.35128 -0.486446206
52   Oceania 1967  2 1.000000e+00 75.75236 -0.657621449
53   Oceania 1972  2 1.000000e+00 71.49393  0.061248019
54   Oceania 1977  2 1.000000e+00 59.48129  1.959693154
55   Oceania 1982  2 1.000000e+00 65.16196  1.333730586
56   Oceania 1987  2 1.000000e+00 55.42689  2.897375599
57   Oceania 1992  2 1.000000e+00 64.94771  1.741405884
58   Oceania 1997  2 1.000000e+00 65.60108  1.819983594
59   Oceania 2002  2 1.000000e+00 67.22949  1.802273891
60   Oceania 2007  2 1.000000e+00 70.40454  1.481522085

Simplifying plots with plyr

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

col.table<-c("red","blue","green","orange")
gapminder.1982<-subset(gapminder,year==1982)

add.trend.line<-function(x,y,d,...){
  fit<-lm(d[[y]]~log10(d[[x]]))
  abline(fit,...)
}

#recall functions and objects created in the functions lesson
colour.by.category<-function(x,table){
  unname(table[x])
}
rescale<-function(x,r.out){
  p<-(x-min(x))/(max(x)-min(x))
  r.out[[1]]+p*(r.out[[2]]-r.out[[1]])
}
col<-colour.by.category(gapminder.1982$continent,col.table)
cex<-rescale(sqrt(gapminder.1982$pop),c(0.2,10))
#now use function add.trend.line
plot(lifeExp~gdpPercap,gapminder.1982,log="x",cex=cex,col=col,pch=21)
d_ply(gapminder.1982,.(continent),function(x)add.trend.line("gdpPercap","lifeExp",x,col=col.table[x$continent]))

Summarise

library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

summarise(gapminder,pop.mean=mean(pop),pop.var=var(pop),pop.max=max(pop))
# A tibble: 1 x 3
   pop.mean pop.var    pop.max
      <dbl>   <dbl>      <int>
1 29601212. 1.13e16 1318683096
#But if you want to split by groups, you need to combine with ddply

ddply(gapminder,.(continent,year),summarise,pop.mean=mean(pop),pop.var=var(pop),pop.max=max(pop))
   continent year  pop.mean      pop.var    pop.max
1     Africa 1952   4570010 3.991017e+13   33119096
2     Africa 1957   5093033 5.007037e+13   37173340
3     Africa 1962   5702247 6.332252e+13   41871351
4     Africa 1967   6447875 8.073930e+13   47287752
5     Africa 1972   7305376 1.026338e+14   53740085
6     Africa 1977   8328097 1.342165e+14   62209173
7     Africa 1982   9602857 1.810705e+14   73039376
8     Africa 1987  11054502 2.334015e+14   81551520
9     Africa 1992  12674645 3.084491e+14   93364244
10    Africa 1997  14304480 3.949367e+14  106207839
11    Africa 2002  16033152 4.974237e+14  119901274
12    Africa 2007  17875763 6.208931e+14  135031164
13  Americas 1952  13806098 1.045981e+15  157553000
14  Americas 1957  15478157 1.262883e+15  171984000
15  Americas 1962  17330810 1.511408e+15  186538000
16  Americas 1967  19229865 1.757755e+15  198712000
17  Americas 1972  21175368 2.019196e+15  209896000
18  Americas 1977  23122708 2.294799e+15  220239000
19  Americas 1982  25211637 2.631113e+15  232187835
20  Americas 1987  27310159 2.965858e+15  242803533
21  Americas 1992  29570964 3.376682e+15  256894189
22  Americas 1997  31876016 3.848071e+15  272911760
23  Americas 2002  33990910 4.303563e+15  287675526
24  Americas 2007  35954847 4.738089e+15  301139947
25      Asia 1952  42283556 1.282029e+16  556263527
26      Asia 1957  47356988 1.640861e+16  637408000
27      Asia 1962  51404763 1.852394e+16  665770000
28      Asia 1967  57747361 2.351370e+16  754550000
29      Asia 1972  65180977 3.030903e+16  862030000
30      Asia 1977  72257987 3.675171e+16  943455000
31      Asia 1982  79095018 4.265940e+16 1000281000
32      Asia 1987  87006690 5.095549e+16 1084035000
33      Asia 1992  94948248 6.000559e+16 1164970000
34      Asia 1997 102523803 6.882737e+16 1230075000
35      Asia 2002 109145521 7.656382e+16 1280400000
36      Asia 2007 115513752 8.391068e+16 1318683096
37    Europe 1952  13937362 2.974745e+14   69145952
38    Europe 1957  14596345 3.179928e+14   71019069
39    Europe 1962  15345172 3.480619e+14   73739117
40    Europe 1967  16039299 3.780531e+14   76368453
41    Europe 1972  16687835 4.072461e+14   78717088
42    Europe 1977  17238818 4.227360e+14   78160773
43    Europe 1982  17708897 4.397951e+14   78335266
44    Europe 1987  18103139 4.566646e+14   77718298
45    Europe 1992  18604760 4.895925e+14   80597764
46    Europe 1997  18964805 5.174781e+14   82011073
47    Europe 2002  19274129 5.393398e+14   82350671
48    Europe 2007  19536618 5.581285e+14   82400996
49   Oceania 1952   5343003 2.242101e+13    8691212
50   Oceania 1957   5970988 2.799886e+13    9712569
51   Oceania 1962   6641759 3.449829e+13   10794968
52   Oceania 1967   7300207 4.180741e+13   11872264
53   Oceania 1972   8053050 5.250973e+13   13177000
54   Oceania 1977   8619500 5.950532e+13   14074100
55   Oceania 1982   9197425 7.168295e+13   15184200
56   Oceania 1987   9787208 8.372287e+13   16257249
57   Oceania 1992  10459826 9.862122e+13   17481977
58   Oceania 1997  11120715 1.108420e+14   18565243
59   Oceania 2002  11727415 1.222853e+14   19546792
60   Oceania 2007  12274974 1.331452e+14   20434176
library(gapminder)
library(plyr)
library(ggplot2)
library(dplyr)
library(DT)
data(gapminder)

ddply(gapminder,.(continent,year),function(x)sum(x$pop))
   continent year         V1
1     Africa 1952  237640501
2     Africa 1957  264837738
3     Africa 1962  296516865
4     Africa 1967  335289489
5     Africa 1972  379879541
6     Africa 1977  433061021
7     Africa 1982  499348587
8     Africa 1987  574834110
9     Africa 1992  659081517
10    Africa 1997  743832984
11    Africa 2002  833723916
12    Africa 2007  929539692
13  Americas 1952  345152446
14  Americas 1957  386953916
15  Americas 1962  433270254
16  Americas 1967  480746623
17  Americas 1972  529384210
18  Americas 1977  578067699
19  Americas 1982  630290920
20  Americas 1987  682753971
21  Americas 1992  739274104
22  Americas 1997  796900410
23  Americas 2002  849772762
24  Americas 2007  898871184
25      Asia 1952 1395357351
26      Asia 1957 1562780599
27      Asia 1962 1696357182
28      Asia 1967 1905662900
29      Asia 1972 2150972248
30      Asia 1977 2384513556
31      Asia 1982 2610135582
32      Asia 1987 2871220762
33      Asia 1992 3133292191
34      Asia 1997 3383285500
35      Asia 2002 3601802203
36      Asia 2007 3811953827
37    Europe 1952  418120846
38    Europe 1957  437890351
39    Europe 1962  460355155
40    Europe 1967  481178958
41    Europe 1972  500635059
42    Europe 1977  517164531
43    Europe 1982  531266901
44    Europe 1987  543094160
45    Europe 1992  558142797
46    Europe 1997  568944148
47    Europe 2002  578223869
48    Europe 2007  586098529
49   Oceania 1952   10686006
50   Oceania 1957   11941976
51   Oceania 1962   13283518
52   Oceania 1967   14600414
53   Oceania 1972   16106100
54   Oceania 1977   17239000
55   Oceania 1982   18394850
56   Oceania 1987   19574415
57   Oceania 1992   20919651
58   Oceania 1997   22241430
59   Oceania 2002   23454829
60   Oceania 2007   24549947
ddply(gapminder,.(continent,year),function(x)var(x$pop))
   continent year           V1
1     Africa 1952 3.991017e+13
2     Africa 1957 5.007037e+13
3     Africa 1962 6.332252e+13
4     Africa 1967 8.073930e+13
5     Africa 1972 1.026338e+14
6     Africa 1977 1.342165e+14
7     Africa 1982 1.810705e+14
8     Africa 1987 2.334015e+14
9     Africa 1992 3.084491e+14
10    Africa 1997 3.949367e+14
11    Africa 2002 4.974237e+14
12    Africa 2007 6.208931e+14
13  Americas 1952 1.045981e+15
14  Americas 1957 1.262883e+15
15  Americas 1962 1.511408e+15
16  Americas 1967 1.757755e+15
17  Americas 1972 2.019196e+15
18  Americas 1977 2.294799e+15
19  Americas 1982 2.631113e+15
20  Americas 1987 2.965858e+15
21  Americas 1992 3.376682e+15
22  Americas 1997 3.848071e+15
23  Americas 2002 4.303563e+15
24  Americas 2007 4.738089e+15
25      Asia 1952 1.282029e+16
26      Asia 1957 1.640861e+16
27      Asia 1962 1.852394e+16
28      Asia 1967 2.351370e+16
29      Asia 1972 3.030903e+16
30      Asia 1977 3.675171e+16
31      Asia 1982 4.265940e+16
32      Asia 1987 5.095549e+16
33      Asia 1992 6.000559e+16
34      Asia 1997 6.882737e+16
35      Asia 2002 7.656382e+16
36      Asia 2007 8.391068e+16
37    Europe 1952 2.974745e+14
38    Europe 1957 3.179928e+14
39    Europe 1962 3.480619e+14
40    Europe 1967 3.780531e+14
41    Europe 1972 4.072461e+14
42    Europe 1977 4.227360e+14
43    Europe 1982 4.397951e+14
44    Europe 1987 4.566646e+14
45    Europe 1992 4.895925e+14
46    Europe 1997 5.174781e+14
47    Europe 2002 5.393398e+14
48    Europe 2007 5.581285e+14
49   Oceania 1952 2.242101e+13
50   Oceania 1957 2.799886e+13
51   Oceania 1962 3.449829e+13
52   Oceania 1967 4.180741e+13
53   Oceania 1972 5.250973e+13
54   Oceania 1977 5.950532e+13
55   Oceania 1982 7.168295e+13
56   Oceania 1987 8.372287e+13
57   Oceania 1992 9.862122e+13
58   Oceania 1997 1.108420e+14
59   Oceania 2002 1.222853e+14
60   Oceania 2007 1.331452e+14
ddply(gapminder,.(continent,year),function(x)max(x$pop))
   continent year         V1
1     Africa 1952   33119096
2     Africa 1957   37173340
3     Africa 1962   41871351
4     Africa 1967   47287752
5     Africa 1972   53740085
6     Africa 1977   62209173
7     Africa 1982   73039376
8     Africa 1987   81551520
9     Africa 1992   93364244
10    Africa 1997  106207839
11    Africa 2002  119901274
12    Africa 2007  135031164
13  Americas 1952  157553000
14  Americas 1957  171984000
15  Americas 1962  186538000
16  Americas 1967  198712000
17  Americas 1972  209896000
18  Americas 1977  220239000
19  Americas 1982  232187835
20  Americas 1987  242803533
21  Americas 1992  256894189
22  Americas 1997  272911760
23  Americas 2002  287675526
24  Americas 2007  301139947
25      Asia 1952  556263527
26      Asia 1957  637408000
27      Asia 1962  665770000
28      Asia 1967  754550000
29      Asia 1972  862030000
30      Asia 1977  943455000
31      Asia 1982 1000281000
32      Asia 1987 1084035000
33      Asia 1992 1164970000
34      Asia 1997 1230075000
35      Asia 2002 1280400000
36      Asia 2007 1318683096
37    Europe 1952   69145952
38    Europe 1957   71019069
39    Europe 1962   73739117
40    Europe 1967   76368453
41    Europe 1972   78717088
42    Europe 1977   78160773
43    Europe 1982   78335266
44    Europe 1987   77718298
45    Europe 1992   80597764
46    Europe 1997   82011073
47    Europe 2002   82350671
48    Europe 2007   82400996
49   Oceania 1952    8691212
50   Oceania 1957    9712569
51   Oceania 1962   10794968
52   Oceania 1967   11872264
53   Oceania 1972   13177000
54   Oceania 1977   14074100
55   Oceania 1982   15184200
56   Oceania 1987   16257249
57   Oceania 1992   17481977
58   Oceania 1997   18565243
59   Oceania 2002   19546792
60   Oceania 2007   20434176